import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcConn {
    //第一种方式
    @Test
    public void connect01() throws SQLException {
        org.mariadb.jdbc.Driver driver = new org.mariadb.jdbc.Driver();
        String url = "jdbc:mariadb://localhost:3306/lfm_db02";
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "lfm");
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
    }

    //方式2
    @Test
    public void connect02() throws ClassNotFoundException,  InstantiationException, IllegalAccessException, SQLException{

        //使用反射加载Driver类, 动态加载， 更加灵活，减少依赖性
        Class<?> aClass = Class.forName("org.mariadb.jdbc.Driver");
        org.mariadb.jdbc.Driver driver = (org.mariadb.jdbc.Driver)aClass.newInstance();


        String url = "jdbc:mariadb://localhost:3306/lfm_db02";
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "lfm");
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
    }
    //方式3 使用DriverManager 替代 Driver进行统一管理
    @Test
    public void connect03() throws IllegalAccessException, InstantiationException, ClassNotFoundException, SQLException {

        Class<?> aClass = Class.forName("org.mariadb.jdbc.Driver");
        org.mariadb.jdbc.Driver driver  = (org.mariadb.jdbc.Driver)aClass.newInstance();

        Properties properties = new Properties();
        String url = "jdbc:mariadb://localhost:3306/lfm_db02";
        String user = "root";
        String password = "lfm";

        DriverManager.registerDriver(driver);

        java.sql.Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("第三种方式 " + connection );

    }

    //方式4 使用Class.forname 自动完成注册驱动
    @Test
    public void connect04() throws ClassNotFoundException, SQLException {

        //在加载Driver类时，完成注册
        /*
            源码:
            1.
         */
        Class.forName("org.mariadb.jdbc.Driver");

        String url = "jdbc:mariadb://localhost:3306/lfm_db02";
        String user = "root";
        String password = "lfm";

        java.sql.Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println("第4种" + connection);

    }
    //方式5,在方式4的基础上改进，增加配置文件，让连接mysql更加灵活
    @Test
    public void connect05() throws IOException, ClassNotFoundException, SQLException {
        //通过Properties对象获取
        Properties properties = new Properties();
        properties.load(new FileInputStream("mariadb.properties"));

        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        Class.forName(driver);

        java.sql.Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println("方式5" + connection);

    }


    public static void main(String[] args) {

    }

}
